package com.mytest.demo.jpa;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mytest.demo.entity.Role;
import com.mytest.demo.entity.User;

@RestController
@RequestMapping("/jpa/user")
public class JPAController {
	@Autowired
	private UserDao userDao;
	
	@GetMapping
	public String getAll() {
		List<User>ls=userDao.findAll();
		
		return JSON.toJSONString(ls);
	}
	
	@RequestMapping("/add")
	public String addUser(User user){
		userDao.save(user);
		return "saved";
	}
	
	@RequestMapping("/get")
	public String getUserRole(String username){
		Role r=userDao.findById(1).get().getRole();
		
		return r.getRoleName();
	}
}
